package com.seeyon.apps.kk;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.ddkk.timer.GetReportTimers;
import com.seeyon.apps.kk.constants.Constants;
import com.seeyon.apps.kk.timer.SynDataTimers;
import com.seeyon.apps.kk.timer.SynMemberTimers;
import com.seeyon.ctp.common.AbstractSystemInitializer;
import com.seeyon.ctp.common.timer.TimerHolder;

public class QiangsBasePluginInitializer extends AbstractSystemInitializer {

	private static final Log log = LogFactory.getLog(QiangsBasePluginInitializer.class);
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub
		log.info("*****************开始初始化强生客开插件******************");
//		FormDataBaseManager baseFormQiangsManager = (FormDataBaseManager) AppContext.getBean("baseFormQiangsManager");
//		String json = baseFormQiangsManager.fltTemplateIntoString(null, "/kkBase/configJson.ftl", null);
		Thread synMember = new Thread(new Runnable() {
			@Override
			public void run() {
				if("不执行".equals(Constants.RUNNING)){
					return ;
				} else if("立即执行".equals(Constants.RUNNING)) {
	                new SynMemberTimers().run();
	            }
	            TimerHolder.newTimer(new SynMemberTimers(), getFirstRunDate(), getRunPeriod());
	            log.info("*****************初始化：	人员同步定时任务******************");
			}
    	});
		synMember.start();
		Thread synData = new Thread(new Runnable() {
			@Override
			public void run() {
	            TimerHolder.newTimer(new SynDataTimers(), new Date(), 60 * 60 * 1000);
	            log.info("*****************初始化：	第三方数据同步定时任务******************");
			}
    	});
		synData.start();
    	log.info("*****************完成初始化强生客开插件******************");
	}

	/**
     * 获取第一次执行定时任务的时间 如果第一次执行定时任务的时间 小于当前的时间 此时要在
     * 第一次执行定时任务的时间加一天，以便此任务在下个时间点执行。如果不加一天，任务会立即执行。
     * @return 第一次启动任务时间
     */
    private static Date getFirstRunDate() {
        String period = Constants.PERIOD;
        Calendar calendar = Calendar.getInstance();
        if("每五分钟".equals(period)) {
            calendar.add(Calendar.MINUTE, 5);
        } else if("每一小时".equals(period)) {
            calendar.add(Calendar.HOUR, 1);
        } else if("每天0点".equals(period)) {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            if(calendar.getTime().before(new Date())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        } else if("每天4点".equals(period)) {
            calendar.set(Calendar.HOUR_OF_DAY, 4);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            if(calendar.getTime().before(new Date())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        } else if("每天8点".equals(period)) {
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            if(calendar.getTime().before(new Date())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        } else if("每天12点".equals(period)) {
            calendar.set(Calendar.HOUR_OF_DAY, 12);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            if(calendar.getTime().before(new Date())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        } else if("每天16点".equals(period)) {
            calendar.set(Calendar.HOUR_OF_DAY, 16);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            if(calendar.getTime().before(new Date())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 20);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            if(calendar.getTime().before(new Date())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
        return calendar.getTime();
    }

    /**
     * 获取执行周期
     * @return 执行周期
     */
    private long getRunPeriod() {
        long runPeriod = 0L;
        String period = Constants.PERIOD;
        if("每五分钟".equals(period)) {
            runPeriod = 5 * 60 * 1000;
        } else if("每一小时".equals(period)) {
            runPeriod = 60 * 60 * 1000;
        } else {
            runPeriod = 24 * 60 * 60 * 1000;
        }
        return runPeriod;
    }
}
